﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace Prototype
{
    public partial class rss : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Sources: http://www.dotnettutorials.com/tutorials/advanced/rss-feed-asp-net-vb/

            Response.Clear();
            Response.ContentType = "application/rss+xml";
            XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("rss");
            xmlWriter.WriteAttributeString("version", "2.0");
            xmlWriter.WriteStartElement("channel");

            string sql = @"SELECT Article.ArticleName, Article.ArticleImageUrl, ApplicationUser.UserName, Article.ArticleID, Step.StepURL
FROM (Article INNER JOIN ApplicationUser ON Article.UserID =  ApplicationUser.UserID) INNER JOIN Step ON Article.ArticleID = Step.ArticleID";
            OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["AutoFixConnectionString"].ToString());
            OleDbCommand cmdSelectAllArticles = new OleDbCommand(sql, connection);
            OleDbDataReader dataReader = null;
            connection.Open();
            dataReader = cmdSelectAllArticles.ExecuteReader();
            dataReader.Read();
            if (dataReader.HasRows)
            {

                xmlWriter.WriteElementString("title", "AutoFix Article RSS Feed");
                xmlWriter.WriteElementString("link", "http://autofix.codeplex.com");
                xmlWriter.WriteElementString("description", "AutoFix site enables you to fix your mitsuibishi vehicle by yourself. We have a variety of articles that can assist you in repairing your vehicle.");
                xmlWriter.WriteElementString("language", "en-us");
                xmlWriter.WriteElementString("lastBuildDate", String.Format("{0:R}", DateTime.Now));

                while (dataReader.Read())
                {
                    xmlWriter.WriteStartElement("item");
                    xmlWriter.WriteElementString("title", dataReader.GetValue(0).ToString());
                    xmlWriter.WriteElementString("author", dataReader.GetValue(2).ToString());
                    xmlWriter.WriteElementString("link", "details.aspx?aID=" + dataReader.GetValue(3).ToString());
                    xmlWriter.WriteElementString("description", GetDescription(dataReader.GetValue(4).ToString())+"...");
                    xmlWriter.WriteEndElement();
                }

                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndDocument();
                xmlWriter.Flush();
                xmlWriter.Close();
                Response.End();
            }
        }
        /// <summary>
        /// Gets the step 1 content from the step path
        /// </summary>
        /// <param name="pathToStepUrl">step content url path</param>
        /// <returns>step content</returns>
        private string GetDescription(string pathToStepUrl)
        {
            string stepContent = string.Empty;
            StreamReader stepContentReader = new StreamReader(Server.MapPath(pathToStepUrl));
            stepContent = stepContentReader.ReadLine();
            stepContentReader = null;
            return stepContent;
        }

    }
}